package com.walkline.travelassistant.dao;

import java.util.Vector;

import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;

import com.walkline.travelassistant.TravelAssistantException;
import com.walkline.travelassistant.TravelAssistantSDK;
import com.walkline.travelassistant.inf.Route;
import com.walkline.travelassistant.inf.RouteWrapper;
import com.walkline.travelassistant.inf.Scheme;
import com.walkline.util.Function;

public class TravelAssistantRouteWrapper extends TravelAssistantObject implements RouteWrapper
{
	private Vector _route_list = new Vector();
	private Vector _scheme_list = new Vector();
	private String _origin_name = "";
	private String _destination_name = "";
	private int _route_count = 0;
	private boolean _has_scheme = false;

	public TravelAssistantRouteWrapper(TravelAssistantSDK travelAssistant, JSONObject jsonObject) throws TravelAssistantException
	{
		super(travelAssistant, jsonObject);

		JSONObject routeWrapper = jsonObject;
		if (routeWrapper != null)
		{
			JSONArray routes = routeWrapper.optJSONArray("routes");
			if (routes != null)
			{
				JSONArray schemes;
				JSONObject routeObject;
				JSONObject schemeObject;
				Route route;
				Scheme scheme;

				_route_count = routes.length();
				for (int i=0; i<_route_count; i++)
				{
					try {
						routeObject = (JSONObject) routes.get(i);

						if (routeObject.isNull("scheme"))
						{
							route = new TravelAssistantRoute(travelAssistant, routeObject);
							if (route != null) {_route_list.addElement(route);}							
						} else {
							_has_scheme = true;
							schemes = routeObject.optJSONArray("scheme");
							if (schemes != null)
							{
								 schemeObject = (JSONObject) schemes.get(0);
								 scheme = new TravelAssistantScheme(travelAssistant, schemeObject);
								 if (scheme != null) {_scheme_list.addElement(scheme);}
							}
						}
					} catch (JSONException e) {Function.errorDialog(e.toString());}
				}
			}

			_origin_name = routeWrapper.optJSONObject("origin").optString("wd");
			_destination_name = routeWrapper.optJSONObject("destination").optString("wd");
		}
	}

	public Vector getRoutes() {return _route_list;}

	public int getRoutesCount() {return _route_count;}

	public Vector getSchemes() {return _scheme_list;}

	public boolean hasScheme() {return _has_scheme;}

	public String getOriginName() {return _origin_name;}

	public String getDestinationName() {return _destination_name;}
}